<?php
/* explain:		news system
 * Project:     
 * File:        news.php
 *
 * @ link 		http://www.../modules/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2005  Ling Deming
 * @ author 	ivan.ling  Ling Deming
 * @ version 	1.0
 *
 */
include_once("utils/CommonUtils.php");
include_once("domain/CouponAttributeDomain.php");
include_once("dao/CouponAttributeDAO.php");
class CouponAttributeImpl extends CommonUtils implements CouponAttributeDAO
{

	function __construct($DBTag)
	{
		parent::__construct();
		
		$this->DBTag = $DBTag;	
	}

	/* get data via ID
	 *
	 * @param  int    id
	 * @return mixed  false:
	 */
	public function getById($id)
	{		
		$arrRt	= null;
		$conn 	= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql 	= "SELECT * FROM coupon_attributes WHERE id = $id";
			$rs  	= mysql_query($sql, $conn);
			if($rs)
				$arrRt = mysql_fetch_array($rs);
			else
				$arrRt = false;
		}catch(Exception $e){
			$this->messages = $e->getMessage();
			$arrRt = false;						
		}
		
		$this->freeConnection($this->DBTag, $conn);		
		return $arrRt;
	}

	public function add($oAttr)
	{		
		if($oAttr instanceof CouponAttributeDomain){
			$IDkey = md5($oAttr->getMerchantId() . $oAttr->getCode() . $oAttr->getAmount() . $oAttr->getDescript());
			
			$sql 	 = "INSERT INTO tag SET modifiedTime = UTC_TIMESTAMP(), descript='";
			$sql	.= addslashes($oAttr->getDescript());
			
			$sql 	.= "', comments='";
			$sql	.= addslashes($oAttr->getComments());
	
			$sql 	.= "', amount='";
			$sql	.= addslashes($oAttr->getAmount());
	
			$sql 	.= "', merchantId=";
			$sql	.= $oAttr->getMerchantId();

			$sql 	.= ", categoryId=";
			$sql	.= $oAttr->getCategoryId();
			
			$sql	.= ", expireDate='";
			$sql	.= $oAttr->getExpireDate();

			$sql	.= "', code='";
			$sql	.= $oAttr->getCode();

			$sql	.= "', IDkey='";
			$sql	.= $IDkey;
			
			$sql	.= "', status=";
			$sql	.= $oAttr->getStatus();
			$sql	.= "";
			
			$id = $this->insert($this->DBTag, $sql);
			
			$oAttr->setId($id);
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of AttributeDomain.';
		}
			
		return $id;
	}
	
	/* edit
	 *
	 * @param  string 	userid		news class name 
	 * @param  string 	number		man number  
	 * @param  string  	position    position
	 * @param  string 	summary		summary 
	 * @param  string 	descript	descript  
	 * @param  datetime	startDate	start date
	 * @param  datetime endDate		end date
 	 	 * @param  return 	boolean
	 */
	public function edit($oAttr)
	{
		$merchantId = $oAttr->getMerchantId();
		$code		= $oAttr->getCode();
		$amount		= $oAttr->getAmount();
		$descript	= $oAttr->getDescript();
		
		$IDkey = md5($merchantId . $code . $amount . $descript);
		
		$sql = "UPDATE tag SET merchantID='$merchantId', categoryID='$categoryID', code = '$code', descript = '$descript', URL = '$URL', expireDate = '$expireDate', " .
				"startDate = '$startDate', startPrice = '$startPrice', longRestr = '$longRestr', amount = '$amount', isFeatured = '$isFeatured', " .
				"isFreeShipping = '$isFreeShipping', isFreeGift = '$isFreeGift', isFreeSample = '$isFreeSample', isClearanceSales = '$isClearanceSales', " .
				"status = '$status', comments = '$comments', IDkey = '$IDkey', createdTime = UTC_TIMESTAMP() WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	/* delete news class
	 *
	 * @param  int    	id
	 * @param  string  	status
	 * @param  return 	boolean
	 */
	public function delete($id)
	{
		$sql = "DELETE FROM coupon WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	
	/* list
	 *
	 * @ param  string 	class id
	 * @ param  string 	page
	 * @ param  string 	discount
	 * @ param  string 	url
	 * @ param  return 	array
	 */
	public function getLists($couponId)
	{
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($this->DBTag);
			$sql  	= 'SELECT ca.id, ca.attributeId, ca.attributeValue, a.name attributeName';
			$sql 	.= ' FROM coupon_attributes ca, attributes a WHERE a.id = ca.attributeId AND ca.couponId = ' . $couponId;
			
			$rs  = mysql_query($sql, $conn);
			if($rs){
				while($row = mysql_fetch_array($rs)){
					$oAttr = new CouponAttributeDomain();

					$oAttr->setId($row[0]);
					
					$oAttr->setAttributeId($row[1]);
					$oAttr->setAttributeValue($row[2]);
					$oAttr->setAttributeName($row[3]);
					
					$oAttr->setCouponId($couponId);
					
					$arrback[$row[1]] = $oAttr;
				}
			}else {
				$this->message = "select failed. sql : $sql";
				$this->message .= "<br>error: " . mysql_error($conn);

			}			
		}catch(Exception $e){
			$this->message = $e->getMessage();			
		}
		
		$this->freeConnection($this->DBTag, $conn);
		return $arrback;
	}
	
	public function getMessage() 
	{
		return $this->message;
	}

	
	
}
?>
